package hoang.hut.app.utility;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FontUtility {
    /**
     * set font from folder assets cho edittext
     * 
     * @param context
     * @param edittext
     * @param path: "fonts/Fears.ttf"
     */
    public static void setFontEditText(Context context, EditText edittext, String path){
    	Typeface tf = Typeface.createFromAsset(context.getAssets(), path);
    	edittext.setTypeface(tf);
    }
    /**
     *  set font from folder assets cho Textview
     * 
     * @param context
     * @param textview
     * @param path: from assets: "fonts/Fears.ttf"
     */
    public static void setFontTextView(Context context, TextView textview, String path){
		Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), path);
		textview.setTypeface(myTypeface);
	}
    /**
     *  set font from folder assets cho Button
     * 
     * @param context
     * @param textview
     * @param path: from assets: "fonts/Fears.ttf"
     */
    public static void setFontTextView(Context context, Button button, String path){
		Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), path);
		button.setTypeface(myTypeface);
	}
    /**
	 * Recursively sets a {@link Typeface} to all
	 * {@link TextView}s in a {@link ViewGroup}.
	 */
	public static final void setAppFont(Context context, ViewGroup mContainer, String font_path)
	{
		Typeface mFont = Typeface.createFromAsset(context.getAssets(),font_path); 
		
	    if (mContainer == null || mFont == null) return;

	    final int mCount = mContainer.getChildCount();

	    // Loop through all of the children.
	    for (int i = 0; i < mCount; ++i)
	    {
	        final View mChild = mContainer.getChildAt(i);
	        if (mChild instanceof TextView)
	        {
	            // Set the font if it is a TextView.
	            ((TextView) mChild).setTypeface(mFont);
	        }
	        else if (mChild instanceof ViewGroup)
	        {
	            // Recursively attempt another ViewGroup.
	            setAppFont(context, (ViewGroup) mChild, font_path);
	        }
	    }
	}
}
